package asteroids.engine;

import java.awt.Graphics;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import asteroids.entity.Asteroid;

public class AsteroidManager implements ObjectManager<Asteroid>
{
    static Logger logger = Logger.getLogger(AsteroidManager.class);

    private ArrayList<Asteroid> asteroids;

    public AsteroidManager()
    {
        asteroids = new ArrayList<Asteroid>();
    }

    public void add(Asteroid a)
    {
        asteroids.add(a);
    }

    public void addAll(ArrayList<? extends Asteroid> as)
    {
        asteroids.addAll(as);
    }

    public ArrayList<Asteroid> getObjects()
    {
        ArrayList<Asteroid> as = new ArrayList<Asteroid>();
        for( Asteroid a : asteroids )
        {
            as.addAll(a.getObjects());
        }
        return as;
    }

    /**
     * a more-conveneniently-named dupe of getObjects()
     * 
     * @return
     */
    public ArrayList<Asteroid> getAsteroids()
    {
        return asteroids;
    }

    public void update()
    {
        for( int i = asteroids.size() - 1; i >= 0; i-- )
        {
            Asteroid a = asteroids.get(i);
            // a.update();
            if( a.isActive() )
            {
                a.update();
            }
            else
            {
                asteroids.remove(i);
                a = null;
            }
        }
    }

    public void randomizeAsteroidLocations()
    {
        for( Asteroid a : getObjects() )
        {
            a.setPosition(Math.random() * Utils.GAME_WIDTH, Math.random() * Utils.GAME_HEIGHT);
        }
    }

    public void clear()
    {
        for( Asteroid a : asteroids )
        {
            a.clear();
        }
        asteroids.clear();
    }

    public void erase(Graphics g)
    {
        for( Asteroid a : asteroids )
        {
            a.erase(g);
        }
    }

    public void draw(Graphics g)
    {
        for( Asteroid a : asteroids )
        {
            a.draw(g);
        }
    }
}
